import { LoginSystem } from '@/axios/authorize'
import { getToken, setToken } from '@/store/localStorage/cookies'

const app={
    namespaced: false,
    state: () => ({
        AccessToken: getToken()
    }),
    getters: {
        AccessToken: state => state.AccessToken
    },
    mutations: {
        SET_TOKEN: (state,token) => {
            state.AccessToken = token
        }
    },
    actions: {
        login ({commit},userInfo){
            const {username, password} = userInfo;
            return new Promise((resolve,reject) => {
                LoginSystem({ username: username.trim(), password: password }).then(response => {
                    const { data } = response
                    commit('SET_TOKEN', data.token)
                    setToken(data.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            });
        }
    }
}

export default app;